package demo1;


/**
 * @author
 */

//实现多态
class Animal {
    public String name;
    public int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void eat() {
        System.out.println(this.name + " 吃粮");
    }
}


class cat extends Animal {
    public String color;

    public cat(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat() {
        System.out.println(this.name + "在吃猫粮");
    }
}

class dog extends Animal {
    public String color;

    public dog(String name, int age) {
        super(name, age);
    }

    public void eat() {
        System.out.println(this.name + "在吃狗粮");
    }
}
public class Test {
    public static void func(Animal animal) {
        animal.eat();
    }
    public static void main(String[] args) {
        //多态的使用条件： 1.向上转型
        Animal animal = new cat("miaomiao",1);
        Animal animal2 = new dog("wangwang",2);

        //2.通过父类的引用调用子类和父类中重写的方法
        func(animal);
        func(new dog("wang",3));
    }
}
